package com.vcredit.creditcat.manager.creditcard;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.bird.enumtype.CreditCardBankEnum;
import com.vcredit.bird.service.creditcard.CreditCardProcessService;
import com.vcredit.bird.vo.creditcard.CardInfo;
import com.vcredit.creditcat.api.ICreditCardProcessManager;
import com.vcredit.creditcat.dto.CustCardDto;
import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.enumtype.BankCardTypeEnum;
import com.vcredit.creditcat.enumtype.CreditStatusEnum;
import com.vcredit.creditcat.exception.CheckBankCardException;
import com.vcredit.creditcat.manager.CreditCatProcessManager;
import com.vcredit.creditcat.manager.CustCardManager;
import com.vcredit.creditcat.model.AuthCreditcard;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.AuthLog;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.service.AuthCreditcardService;
import com.vcredit.creditcat.service.AuthLogService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.util.EnumUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class CreditCardProcessManager implements ICreditCardProcessManager {

	@Autowired
	private AuthCreditcardService authCreditcardService;

	@Autowired
	private CreditCardProcessService creditCardProcessService;

	@Autowired
	private CreditCatProcessManager creditCatProcessManager;
	@Autowired
	private CustCardManager custCardManager;
	@Autowired
	private AuthLogService authLogService;

	@Value("${card.three.guid}")
	private String source;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void validCreditCard(Cust cust, String bankCode, String bankCardNo) throws Exception {

		AuthCreditcard creditCard = authCreditcardService.findByProcessId(cust.getLastProccessId());
		// 验卡
		if (!creditCardProcessService.verifyCard(bankCardNo, cust.getCustName(), cust.getCustIdno(), null,
				EnumUtil.valueOf(CreditCardBankEnum.class, bankCode, "vbsEnum"), source)) {
			throw new CheckBankCardException("信用卡与身份信息不一致");
		}
		CardInfo cardInfo = creditCardProcessService.getUnionCardInfo(cust.getCustIdno(), bankCardNo, null,
				Constants.CREDIT_CARD_BUSTYPE);// 获取银联数据
		AuthCreditcard param = new AuthCreditcard(cust.getLastProccessId(), bankCardNo, bankCode,
				CreditStatusEnum.SUCCESS.getCode(), ConcurrentDateUtil.formatDateTime(new Date()), cardInfo.getReportId());
		// 绑卡
		param.setExpiredTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(), 30)));
		CustCardDto cardDto = new CustCardDto();
		cardDto.setAccountName(cust.getCustName());
		cardDto.setAccountNo(bankCardNo);
		cardDto.setBankCode(bankCode);
		cardDto.setCardType(BankCardTypeEnum.CREDIT);
		custCardManager.bindCustCard(cust, cardDto);
		creditCatProcessManager.itemDone(new AuthItem(AuthItemEnum.CREDIT_CARD.getCode()), param.getProcessId());
		authLogService.insert(new AuthLog(param.getProcessId(), cust.getCustId(), AuthItemEnum.CREDIT_CARD.getCode(),
				param.getExpiredTime(), new Date()));
		if (null != creditCard) {

			param.setCreditcardProcessId(creditCard.getProcessId());
			authCreditcardService.update(param);
		} else {
			authCreditcardService.insert(param);
		}
	}

}
